package android.longs.study.sign.dao;

import java.util.List;

import org.hibernate.Session;

import android.longs.study.sign.model.UserSignModel;
import android.longs.study.utils.H3Utils;

public class UserSignDaoImple implements UserSignDao {

    @Override
    public UserSignModel addUserSignModel(UserSignModel model) {

	Session currentSession = H3Utils.getCurrentSession();
	currentSession.save(model);
	return null;
    }

    @Override
    public List<UserSignModel> queryAllFromUserId(String userId) {
	Session currentSession = H3Utils.getCurrentSession();
	List<UserSignModel> list = currentSession.createQuery("from UserSignModel u where u.signUserUid='"+userId+"'").list();
	return list;
    }

    @Override
    public UserSignModel querSignModelFromId(int id) {
	Session currentSession = H3Utils.getCurrentSession();
	UserSignModel model  = (UserSignModel) currentSession.createQuery("from UserSignModel u where u.signId='"+id+"'").uniqueResult();
	return model;
    }

    @Override
    public UserSignModel querSignModelFromUId(String uid) {
	Session currentSession = H3Utils.getCurrentSession();
	UserSignModel model  = (UserSignModel) currentSession.createQuery("from UserSignModel u where u.signUid='"+uid+"'").uniqueResult();
	return model;
    }


    @Override
    public UserSignModel querSignModelSignInLast(String userId) {
	Session currentSession = H3Utils.getCurrentSession();
	List<UserSignModel> list = currentSession.createQuery("from UserSignModel c where c.signType=1 and c.signUserUid = '"+userId+"'  order by c.signId asc").list();
	if (list!=null&&!list.isEmpty()) {
	    UserSignModel userSignModel = list.get(list.size()-1);
	    return userSignModel;
	}
	return null;
    }

    @Override
    public UserSignModel querSignModelSignOutLast(String userId) {
	Session currentSession = H3Utils.getCurrentSession();
	List<UserSignModel> list = currentSession.createQuery("from UserSignModel c where c.signType=2 and c.signUserUid = '"+userId+"'  order by c.signId asc").list();
	if (list!=null&&!list.isEmpty()) {
	    UserSignModel userSignModel = list.get(list.size()-1);
	    return userSignModel;
	}
	return null;
    }

    @Override
    public List<UserSignModel> queryAllFromUserId(String userId, int index,int type ) {
	Session currentSession = H3Utils.getCurrentSession();
	String hql ;
	if (type==1) {
	    hql = "from UserSignModel u where u.signUserUid='"+userId+"'  and  u.signType=1 "+" order by u.signId desc";
	} else if (type==2){
	    hql = "from UserSignModel u where u.signUserUid='"+userId+"'   and  u.signType=2 "+" order by u.signId desc";
	}else {
	    hql = "from UserSignModel u where u.signUserUid='"+userId+"'";
	}
	List<UserSignModel> list = 
		currentSession.createQuery(hql)
		.setFirstResult(index)//查询的页数
		.setMaxResults(20)//每页查询的个数 
		.list();

	return list;
    }

    @Override
    public List<UserSignModel> queryAllFromUserClassId(String classId,
	    int index, int typeNumber) {
	Session currentSession = H3Utils.getCurrentSession();
	List<UserSignModel> list = currentSession.createQuery("from UserSignModel u where u.signUserClassId='"+classId+"'   order by u.signId desc").setFirstResult(index).setMaxResults(typeNumber).list();
	return list;
    }

    @Override
    public void updateUserSignModel(UserSignModel model) {
	Session currentSession = H3Utils.getCurrentSession();
	currentSession.saveOrUpdate(model);
	
    }

    @Override
    public List<UserSignModel> queryAll() {
	Session currentSession = H3Utils.getCurrentSession();
	List<UserSignModel> list = currentSession.createQuery("from UserSignModel ").list();
	return list;
    }

}
